/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.CommResult;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.RecommendBasicMapper;
import com.rzico.basics.mapper.RecommendTagMapper;
import com.rzico.basics.model.RecommendVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <pre>
 * 会员业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class RecommendTagService extends BaseServiceImpl<RecommendTag, String> {

    @Autowired
    private RecommendTagMapper recommendTagMapper;

    @Autowired
    private RecommendBasicMapper recommendBasicMapper;

    @Override
    public BaseMapper<RecommendTag, String> getMapper() {
        return recommendTagMapper;
    }

    public List<Recommend> getRecommendByTags(Map<String, Object> params) {
        List<Recommend> list = recommendTagMapper.getRecommendByTags(params);
        return list;
    }
//
//    @Transactional(rollbackFor = Exception.class)
//    public void save(List<Recommend> recommends, Long tagId, Enterprise enterprise) throws Exception {
//        RecommendTag recommendTag = null;
//        for (Recommend recommend : recommends) {
//            recommend.setEnterpriseId(enterprise.getId());
//            recommend.setCreateDate(new Date());
//            int affectCount = recommendBasicMapper.insertUseGeneratedKeys(recommend);
//            if (affectCount <= 0) {
//                throw new Exception("推荐插入失败");
//            }
//            recommendTag = new RecommendTag();
//            recommendTag.setRecommends(recommend.getId());
//            recommendTag.setTags(tagId);
//            int insert = recommendTagMapper.insert(recommendTag);
//            if (insert <=0 ){
//                throw new Exception("推荐插入失败");
//            }
//        }
//    }


//    @Transactional(rollbackFor = Exception.class)
//    public void update(List<Recommend> recommends,Long tagId) throws Exception {
//        Map<String,Object> recommendTagMap = new HashMap<>();
//        RecommendTag recommendTag = null;
//        for (Recommend recommend : recommends) {
//            int affectCount = recommendBasicMapper.updateByPrimaryKeySelective(recommend);
//            if (affectCount <= 0) {
//                throw new Exception("更新失败");
//            }
//
//            recommendTagMap.clear();
//            recommendTagMap.put("recommends",recommend.getId());
//            recommendTagMap.put("tagIds",tagId);
//            recommendTagMapper.deleteByRecommendIdAndTagId(recommendTagMap);
//            recommendTag = new RecommendTag();
//            recommendTag.setRecommends(recommend.getId());
//            recommendTag.setTags(tagId);
//            int insert = super.insert(recommendTag);
//            if (insert <= 0) {
//                throw new Exception("更新失败");
//            }
//        }
//    }

    /**
     * 保存单个的记录
     * @param recommend
     * @param tagId
     * @param enterprise
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCorrelationRecommend(Recommend recommend, Long tagId, Enterprise enterprise) throws Exception {
        RecommendTag recommendTag = null;  //        super.insertUseGeneratedKeys()
        Long recommendId = null;
        //recommend是否存在
        List<Recommend>  recommendList = findRecommendByType(recommend);
        if (recommendList == null || recommendList.size() <= 0) {
            recommend.setEnterpriseId(enterprise.getId());
            recommend.setCreateDate(new Date());
            Integer affectCount = recommendBasicMapper.insertUseGeneratedKeys(recommend);
            if (affectCount <= 0) {
                throw new Exception("推荐插入失败");
            }
            recommendId = recommend.getId();
        } else {
            recommendId = recommendList.get(0).getId();
        }

        //查询是否有这条记录
        RecommendTag recommendTagByType = findRecommendTagByType(recommendId, tagId);
        if (recommendTagByType == null) {
            recommendTag = new RecommendTag();
            recommendTag.setRecommends(recommendId);
            recommendTag.setTags(tagId);
            int insert = recommendTagMapper.insert(recommendTag);
            if (insert <=0 ){
                throw new Exception("推荐插入失败");
            }
        }




    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteRecommendTag(Long[] recommends, Long tagId) throws Exception {
        //查出是否有这条关联记录
        for (Long recommend : recommends) {
            RecommendTag recommendTagByType = findRecommendTagByType(recommend, tagId);
            if (recommendTagByType == null) {
                throw new Exception("不存在这条数据");
            }
            super.delete(recommendTagByType);
        }
    }

    public RecommendTag findRecommendTagByType(Long recommend, Long tagId) {
        Map<String,Object> param = new HashMap<>();
        param.put("recommends",recommend);
        param.put("tags",tagId);
        List<RecommendTag> recommendTagByType = recommendTagMapper.getRecommendTagByType(param);
        return recommendTagByType != null && recommendTagByType.size() > 0 ? recommendTagByType.get(0):null;
    }

    public  List<Recommend> findRecommendByType(Recommend recommend) {
        if (recommend == null || recommend.getType() == null|| recommend.getTypeId() == null) {
            return null;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("type",recommend.getType());
        param.put("typeId",recommend.getTypeId());
        return recommendBasicMapper.findByRecommendByType(param);
    }
}
